package com.loadburn.heron.utils;

import com.loadburn.heron.config.GlobalConfig;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author slacrey (scstlinfeng@yahoo.com)
 *         Date: 13-12-27
 */
public class FileUtils implements Serializable {
    private static final long serialVersionUID = -9123512249115108352L;

    public static void readFile(String filePath, Map<String, File> fileMap) {

        File file = new File(filePath);
        if (!file.isDirectory()) {

            String fileString = file.getPath();
            fileMap.put(fileString.substring(fileString.indexOf(GlobalConfig.getWebViewPrefix())), file);
        } else if (file.isDirectory()) {

            String[] tempFilePaths = file.list();
            for (String tempFilePath : tempFilePaths) {

                File tmpFile = new File(filePath + File.separator + tempFilePath);
                if (!tmpFile.isDirectory()) {

                    String fileString = tmpFile.getPath();
                    fileMap.put(fileString.substring(fileString.indexOf(GlobalConfig.getWebViewPrefix())), tmpFile);
                } else if (tmpFile.isDirectory()) {

                    readFile(filePath + File.separator + tempFilePath, fileMap);
                }
            }
        }

    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public static boolean deleteFile(String delPath)
            throws IOException {

        File file = new File(delPath);
        if (!file.isDirectory()) {

            file.delete();
        } else if (file.isDirectory()) {

            String[] tempFileStrings = file.list();
            for (String tempFileString : tempFileStrings) {
                File delFile = new File(delPath + File.separator + tempFileString);
                if (!delFile.isDirectory()) {

                    delFile.delete();
                } else if (delFile.isDirectory()) {

                    deleteFile(delPath + File.separator + tempFileString);
                }
            }
            file.delete();

        }
        return true;
    }


}
